<?php
 	if ( ! defined('BASEPATH')) exit('No direct script access allowed');

 	class MY_Controller extends CI_Controller{

    public $user_data = array();
    public $logged_in = 0;
    public $offset = 10;
    public $share_user = 0;
        function __construct() {
            parent::__construct();

            //判断分享用户
            if(!empty($_SERVER['QUERY_STRING']) && isset($_GET['share'])){
                $this->share_user = $_GET['share'];
                $this->session->set_userdata('share_user', $this->share_user);
            }else{
                $share = $this->session->userdata('share_user');
                if(!empty($share)){
                    $this->share_user =  $this->session->userdata('share_user');
                }
            }

            $curr_url = 'http://'.$_SERVER['HTTP_HOST'].str_replace('/index.php','',$_SERVER['PHP_SELF']);
            // echo str_replace('/index.php','',$_SERVER['PHP_SELF']);
            if(empty($_SERVER['QUERY_STRING'])==false){
                $curr_url = $curr_url.'?'.$_SERVER['QUERY_STRING'];
            }

            if(strstr($_SERVER['PHP_SELF'],"logreg")==false&&strstr($_SERVER['PHP_SELF'],"login_wx")==false&&strstr($_SERVER['PHP_SELF'],"png")==false&&strstr($_SERVER['PHP_SELF'],"jpg")==false){
                $this->session->set_userdata('curr_url', $curr_url);
            }
            

            $this->load->library('session');
            $this->user_data = $this->session->userdata('user_data');
            $this->load->model('User_model');
            if(empty($this->user_data)){
                $this->logged_in = 0;
            }else{
                $user = $this->User_model->get_user_by_id($this->user_data['id']);
                if(empty($user))
                {
                  $this->session->sess_destroy();
                    unset(
                        $_SESSION['id'],
                        $_SESSION['head_image'],
                        $_SESSION['user_name'],
                        $_SESSION['mobile'],
                        $_SESSION['logged_in']
                    );
                    $this->logged_in = 0;
                    exit;
                }
                $this->logged_in = 1;

                $this->smarty->assign("user_data", $this->user_data);
            }
            $this->smarty->assign("logged_in", $this->logged_in);
        }

	}

  class User_Controller extends MY_Controller{

    public $user_data = array();
    public $logged_in = 0;
   
    function __construct() {
        parent::__construct();

        $curr_url = 'http://'.$_SERVER['HTTP_HOST'].str_replace('/index.php','',$_SERVER['PHP_SELF']);
        // echo str_replace('/index.php','',$_SERVER['PHP_SELF']);
        if(empty($_SERVER['QUERY_STRING'])==false){
            $curr_url = $curr_url.'?'.$_SERVER['QUERY_STRING'];
        }
        
        if(strstr($_SERVER['PHP_SELF'],"logreg")==false)
            $this->session->set_userdata('curr_url', $curr_url);

        $this->load->library('session');
        $this->user_data = $this->session->userdata('user_data');

        if(empty($this->user_data)){
//            print_r("null");
            // redirect('/login');exit;
            //用户存在
            header("Location:"."http://{$_SERVER['HTTP_HOST']}/logreg");
            exit;
        }else{
            $this->load->model('User_model');
            $user = $this->User_model->get_user_by_id($this->user_data['id']);
            if(empty($user))
            {
                unset(
                    $_SESSION['id'],
                    $_SESSION['head_image'],
                    $_SESSION['user_name'],
                    $_SESSION['mobile'],
                    $_SESSION['logged_in']
                );
                header("Location:"."http://".$_SERVER['HTTP_HOST']."/logreg");
                exit;
            }
            $this->logged_in = 1;
            $this->smarty->assign("logged_in", $this->logged_in);
            $this->smarty->assign("user_data", $this->user_data);
        }


    }
	}
